﻿using Inventory.API.Application.Command.ZonemanagementCommand;
using Inventory.Errorcode;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.ZonemanagementCommandHandler
{
    public class DeleteZonemanagementCommandHanlder : IRequestHandler<DeleteZonemanagementCommand, ApiResult>
    {

        private readonly ILogger<DeleteZonemanagementCommandHanlder> _logger;

        private readonly IZonemanagementRepository repository;

        public DeleteZonemanagementCommandHanlder(ILogger<DeleteZonemanagementCommandHanlder> logger, IZonemanagementRepository repository)
        {
            _logger = logger;
            this.repository = repository;
        }

        public async Task<ApiResult> Handle(DeleteZonemanagementCommand request, CancellationToken cancellationToken)
        {



            try
            {
                var result =await repository.GetAsync(x=>x.Zonemanagementid==request.Zonemanagementid);

                if (result == null)
                {
                    return ApiResult.Fail("未找到查询id", StatusCodeEnum.NotFound);

                }
                else
                {
                   result.IsDeleted = true;
                    await repository.UpdateAsync(result);

                    return ApiResult.Success(StatusCodeEnum.Success);
                }
            }
            catch (Exception  ex)
            {
                _logger.LogError(ex, "DeleteZonemanagementCommandHanlder - Something went wrong.");
                throw;
            }
        }
    }
}
